package pers.gs.util;
/**
 * DBUtil.java
 */

import java.sql.*;

public class DBUtil {
    private static final String URL = "jdbc:mysql://localhost:3306/stu_test?serverTimezone=GMT%2B8";
    private static final String USERNAME = "root";
    private static final String PWD = "123";
    public static Connection conn = null;
    public static PreparedStatement pstmt = null;
    public static ResultSet rs = null;

    /*
     *获取链接MySQL对象
     */
    public static Connection getConn() throws ClassNotFoundException, SQLException {
        //1.导入驱动程序，加载具体的驱动类
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.与数据库建立连接
        conn = DriverManager.getConnection(URL,USERNAME,PWD);
        return conn;
    }

    /*
     *操作PreparedStatement
     */
    public static PreparedStatement createPreparedStatement(String sql, Object[] objects) throws SQLException {
        pstmt = conn.prepareStatement(sql);
        if (objects != null){
            for (int i = 0; i < objects.length; i++){
                pstmt.setObject(i+1,objects[i]);
            }
        }
        return pstmt;
    }

    /*
     *关闭连接
     * ResultSet、Statement、Connection
     */
    public static void closeAll(ResultSet rs, Statement statement,Connection conn) throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (statement != null) {
            statement.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
    /*
     * 通用增删改
     */
    public static boolean executeUpdate(String sql, Object[] objects){
        try{
            //1~2.获取连接对象
            conn = getConn();

            //3.发送SQL语句，执行
            pstmt = createPreparedStatement(sql,objects);

            int count = pstmt.executeUpdate();//执行，返回受影响行数

            //4.处理结果
            if(count > 0) {
                return true;
            }
            else{
                return false;
            }
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        }catch (SQLException e) {
            e.printStackTrace();
            return false;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            try {
                closeAll(null,pstmt,conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /*
     * 通用查询
     */
    public static ResultSet executeQuery(String sql,Object[] objects){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int result = -1;//记录返回几条数据
        try{
            //1~2.获取连接对象
            conn = getConn();

            //3.发送SQL语句，执行
            pstmt = createPreparedStatement(sql,objects);

            rs = pstmt.executeQuery();//执行

            return rs;
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
